package com.ruoyi.web.controller.api;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.api.UUser;
import com.ruoyi.system.service.IUUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.api.UUserRecharge;
import com.ruoyi.system.service.IUUserRechargeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 用户费用Controller
 * 
 * @author ruoyi
 * @date 2022-04-22
 */
@RestController
@RequestMapping("/api/uUserRecharge")
public class UUserRechargeController extends BaseController
{
    @Autowired
    private IUUserRechargeService uUserRechargeService;

    @Autowired
    private IUUserService userService;

    /**
     * 查询用户费用列表
     */
    @PreAuthorize("@ss.hasPermi('api:uUserRecharge:list')")
    @GetMapping("/list")
    public TableDataInfo list(UUserRecharge uUserRecharge)
    {
        if (!StringUtils.isEmpty(uUserRecharge.getUserPhone())){
            UUser user = new UUser();
            if (uUserRecharge.getUserPhone().contains("@")){
                user.setEmail(uUserRecharge.getUserPhone());
            }else {
                user.setMobile(uUserRecharge.getUserPhone());
            }

            List<UUser> uUsers = userService.selectUUserList(user);
            if (!uUsers.isEmpty()){
                uUserRecharge.setUserId(uUsers.get(0).getId());
            }else {
                uUserRecharge.setUserId(-1L);
            }
        }
        startPage();
        List<UUserRecharge> list = uUserRechargeService.selectUUserRechargeList(uUserRecharge);

        if (!list.isEmpty()){
            for (UUserRecharge u:list) {
                UUser user = userService.selectUUserById(u.getUserId());
                if (user != null){
                    if (user.getMobile() != null){
                        u.setUserPhone(user.getMobile());
                    }else {
                        u.setUserPhone(user.getEmail());
                    }
                }
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出用户费用列表
     */
    @PreAuthorize("@ss.hasPermi('api:uUserRecharge:export')")
    @Log(title = "用户费用", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UUserRecharge uUserRecharge)
    {
        List<UUserRecharge> list = uUserRechargeService.selectUUserRechargeList(uUserRecharge);
        ExcelUtil<UUserRecharge> util = new ExcelUtil<UUserRecharge>(UUserRecharge.class);
        util.exportExcel(response, list, "用户费用数据");
    }

    /**
     * 获取用户费用详细信息
     */
    @PreAuthorize("@ss.hasPermi('api:uUserRecharge:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(uUserRechargeService.selectUUserRechargeById(id));
    }

    /**
     * 新增用户费用
     */
    @PreAuthorize("@ss.hasPermi('api:uUserRecharge:add')")
    @Log(title = "用户费用", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UUserRecharge uUserRecharge)
    {
        return toAjax(uUserRechargeService.insertUUserRecharge(uUserRecharge));
    }

    /**
     * 修改用户费用
     */
    @PreAuthorize("@ss.hasPermi('api:uUserRecharge:edit')")
    @Log(title = "用户费用", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UUserRecharge uUserRecharge)
    {
        return toAjax(uUserRechargeService.updateUUserRecharge(uUserRecharge));
    }

    /**
     * 删除用户费用
     */
    @PreAuthorize("@ss.hasPermi('api:uUserRecharge:remove')")
    @Log(title = "用户费用", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(uUserRechargeService.deleteUUserRechargeByIds(ids));
    }
}
